package com.zzl.admin.controller.admin;

import com.zzl.admin.common.JsonResult;
import com.zzl.admin.controller.base.BaseController;
import com.zzl.admin.entity.admin.Resource;
import com.zzl.admin.service.admin.ResourceService;
import com.zzl.admin.vo.ZtreeView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Description：资源Controller
 * Created by 朱正磊 on 2019-03-26 16:55
 */
@Slf4j
@Controller
@RequestMapping("/admin/resource")
public class ResourceController extends BaseController {

    private final String PATH = "admin/resource/";

    private final ResourceService resourceService;

    @Autowired
    public ResourceController(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    /**
     * description：请求访问资源主页
     * user 朱正磊
     * time 2019-03-27 10:45
     * @return 返回模型视图
     */
    @GetMapping(value = {"/", "/index"})
    public ModelAndView index() {
        return new ModelAndView(PATH + "index");
    }

    /**
     * description：查询资源列表（分页）
     * user 朱正磊
     * time 2019-03-27 10:49
     * @param searchText 查询条件
     * @return 返回响应结果
     */
    @PostMapping("/list")
    @ResponseBody
    public Page<Resource> list(@RequestParam(value = "searchText", required = false) String searchText) {
        return resourceService.findAllByLike(searchText, getPageRequest());
    }

    /**
     * description：请求访问资源新增表单页
     * user 朱正磊
     * time 2019-03-27 11:05
     * @param map 响应数据
     * @return 返回模型视图
     */
    @GetMapping("/add")
    public ModelAndView add(Map<String, Object> map) {
        List<Resource> list = resourceService.findAll();
        map.put("list", list);
        return new ModelAndView(PATH + "form", map);
    }

    /**
     * description：请求访问资源修改表单页
     * user 朱正磊
     * time 2019-03-27 11:05
     * @param id 资源ID
     * @param map 响应数据
     * @return 返回模型视图
     */
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id, Map<String, Object> map) {
        //查询所有资源
        List<Resource> list = resourceService.findAll();
        map.put("list", list);
        //查询待修改资源
        Resource resource = resourceService.findById(id);
        map.put("resource", resource);
        //返回页面地址以及展示数据
        return new ModelAndView(PATH + "form", map);
    }

    /**
     * description：增加或者修改资源
     * user 朱正磊
     * time 2019-03-27 11:05
     * @param resource 资源对象
     * @return 返回响应结果
     */
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult edit(Resource resource) {
        try {
            resourceService.saveOrUpdate(resource);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    /**
     * description：根据主键删除一条记录
     * user 朱正磊
     * time 2019-03-27 11:05
     * @param id 资源ID
     * @return 返回响应结果
     */
    @GetMapping("/delete/{id}")
    @ResponseBody
    public JsonResult delete(@PathVariable Integer id) {
        try {
            resourceService.delete(id);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    /**
     * description：获取角色的权限树
     * user 朱正磊
     * time 2019-03-27 11:20
     * @param roleId 角色ID
     * @return 返回响应结果
     */
    @GetMapping("/tree/{roleId}")
    @ResponseBody
    public List<ZtreeView> tree(@PathVariable Integer roleId) {
        List<ZtreeView> ztreeViewList = resourceService.tree(roleId);
        log.info("【权限树】内容 = {}", ztreeViewList);
        return ztreeViewList;
    }

}
